package com.coin900.coin.cmd.token;

import com.coin900.coin.cmd.ACmd;
import com.coin900.coin.cmd.CmdSign;
import com.coin900.coin.code.CmdCode;
import com.coin900.coin.persist.coin.CoinTickerVO;
import com.coin900.coin.persist.coin.CoinTokenCoinVO;
import com.coin900.coin.persist.fu.FuUserVO;
import com.coin900.coin.service.coin.ICoinTokenCoinService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author shui
 * @create 2018-01-24
 **/
@Controller
public class CoinTokenCoinPutCmd extends ACmd {
    @Override
    public CmdSign execute(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        FuUserVO user = super.getUser(sign);
        String source = sign.getSource();
        List<CoinTokenCoinVO> tokenCoinList = new Gson().fromJson(source, new TypeToken<List<CoinTokenCoinVO>>() {
        }.getType());
        for (CoinTokenCoinVO coinTokenCoinVO : tokenCoinList) {
            Integer dataStatus = coinTokenCoinVO.getDataStatus();
            coinTokenCoinVO.setCMInfor(user);
            coinTokenCoinVO.setDataStatus(dataStatus);
        }
        this.tokenCoinService.insertAndUpdateCoinTokenCoinBatch(tokenCoinList);
        return respSign;
    }

    @Autowired
    private ICoinTokenCoinService tokenCoinService;

    @Override
    public String getCmdCode() {
        return CmdCode.COIN_TOKEN_COIN_PUT;
    }
}
